package com.demo.pattern.prototype;

import java.io.IOException;

/**
 * @author sourire
 * @version 1.0
 * @date 2020/8/19 9:25
 */
public class Test {
    public static void main(String[] args) throws CloneNotSupportedException, IOException {
        //传统模式克隆羊


        /*Sheep sheep1 = new Sheep("tom", 1, "white");
        Sheep sheep2 = new Sheep("tom", 1, "white");
        Sheep sheep3 = new Sheep("tom", 1, "white");
        System.out.println(sheep1);
        System.out.println(sheep2);
        System.out.println(sheep3);*/

        //原型模式
        /*Sheep sheep1 = (Sheep) sheep.clone();
        Sheep sheep2= (Sheep) sheep.clone();
        Sheep sheep3 = (Sheep) sheep.clone();
        System.out.println(sheep1);
        System.out.println(sheep2);
        System.out.println(sheep3);*/

        //测试深拷贝
        //第一种  通过clone方法来实现
        Sheep sheep = new Sheep("tom", 1, "white");
        DeepSheep deepSheep = new DeepSheep("jerry",1);
        deepSheep.setFriend(sheep);

        /*DeepSheep deepSheep1 = (DeepSheep) deepSheep.clone();
        System.out.println(deepSheep.getFriend().hashCode());
        System.out.println(deepSheep.getFriend());
        System.out.println(deepSheep1.getFriend().hashCode());
        System.out.println(deepSheep1.getFriend());
        */
        //第二种方式  通过序列化来实现
        DeepSheep deepSheep1 = (DeepSheep) deepSheep.deepCopy();
        System.out.println(deepSheep.getFriend().hashCode());
        System.out.println(deepSheep.getFriend());
        System.out.println(deepSheep1.getFriend().hashCode());
        System.out.println(deepSheep1.getFriend());
    }
}
